package com.goldcn.user.service;

import com.goldcn.user.dao.CountMapper;
import com.goldcn.user.vo.MainViewCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by zll on 2017/4/26.
 */
@Service
public class DataStaticsService {
    @Autowired
    CountMapper countMapper;
    /**
     * 而得到分行下的组织以及会员信息
     * 包括二级分行数量 以及支行数量 员工总数 (包括新增)
     * @return
     */
    public Object getDepartmentDataByDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long newDay = calendar.getTimeInMillis();
        List<MainViewCountVo> list = countMapper.getMainViewCountVo(newDay);
        List<Map<String,Object>> twoAddList = countMapper.getTwoAddCount(newDay);
        List<Map<String,Object>> subAddList = countMapper.getSubAddCount(newDay);
        List<Map<String,Object>> saleManagerAddList = countMapper.getSaleManagerAddCount(newDay);
        Map<Long,Integer> twoMap = new HashMap<>();
        for(Map<String,Object> map:twoAddList){
            twoMap.put((Long)map.get("branchId"),((Long)map.get("twoAddCount")).intValue());
        }
        Map<Long,Integer> subMap = new HashMap<>();
        for(Map<String,Object> map:subAddList){
            subMap.put((Long)map.get("branchId"),((Long)map.get("subAddCount")).intValue());
        }
        Map<Long,Integer> sleManagerMap = new HashMap<>();
        for(Map<String,Object> map:saleManagerAddList){
            sleManagerMap.put((Long)map.get("branchId"),((Long)map.get("saleManagerAddCount")).intValue());
        }

        for(MainViewCountVo vo:list){
            if(twoMap.get(vo.getBranchId())!=null){
                vo.setTwoAddCount(twoMap.get(vo.getBranchId()));
            }
            if(subMap.get(vo.getBranchId()) != null){
                vo.setSubAddCount(subMap.get(vo.getBranchId()));
            }
            if(sleManagerMap.get(vo.getBranchId()) != null){
                vo.setSaleManagerAddCount(sleManagerMap.get(vo.getBranchId()));
            }
        }
        return list;
    }
}
